aboutsummaryrefslogtreecommitdiffstats
path: root/src/routes/(app)/send/invoice/[id]/+page.server.js
blob: c6b0a9a4337cb9fdb2c34e994e51b6e2d61a94a7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { error, fail, redirect } from "@sveltejs/kit";
import { fd, auth, get, post } from "$lib/utils";

export async function load({ params: { id }, parent, url }) {
  let { user } = await parent();

  let {
    amount,
    address,
    currency,
    hash,
    rate,
    prompt,
    tip,
    text,
    user: recipient,
  } = await get(`/invoice/${id}`);

  if (prompt && tip === null)
    redirect(307, `/${recipient.username}/invoice/${id}/tip`);
  if (recipient.username === user.username)
    error(500, { message: "Cannot send to self" });

  return {
    amount,
    address,
    currency,
    hash,
    tip,
    rate,
    payreq: text,
    recipient,
  };
}

export const actions = {
  default: async ({ cookies, params: { id }, request }) => {
    try {
      let body = await fd(request);
      body.hash = id;

      await post("/payments", body, auth(cookies));
    } catch (e) {
      console.log("payment failed", params, e);
      return fail(400, { message: e.message });
    }

    redirect(307, "/sent");
  },
};